package com.cdn.allmobilestore.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cdn.allmobilestore.R;
import com.cdn.allmobilestore.activity.DetailActivity;
import com.cdn.allmobilestore.adapter.ListAppAdapter;
import com.cdn.allmobilestore.loader.AppCommentsLoader;
import com.cdn.allmobilestore.loader.BaseLoader;
import com.cdn.allmobilestore.loader.ListAppLoader;
import com.cdn.allmobilestore.loader.Pager;
import com.cdn.allmobilestore.loader.ListAppLoader.Result;
import com.cdn.allmobilestore.model.AppInfo;
import com.cdn.allmobilestore.ulti.Constant;
import com.cdn.allmobilestore.ulti.Log;
import com.cdn.allmobilestore.widget.EmptyLoadingView;
import com.cdn.allmobilestore.widget.PageScrollListener;
import com.cdn.allmobilestore.widget.Refreshable;

public class ListAppFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<ListAppLoader.Result>, Refreshable
{
  private static final int LISTAPP_LOADER = 0;
  private ListAppAdapter mListAppAdapter;
  private ListView mListView;
  private EmptyLoadingView mLoadingView;
  private String mCategoryId;
  private String mAgencyId;
  private String mListAppType =  Constant.LISTAPP_TYPE_DOWNLOAD;
  private ListAppLoader mLoader;
  protected boolean mIsReachedBottom;
  private AdapterView.OnItemClickListener mItemClickListner = new AdapterView.OnItemClickListener()
  {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
    	if (position < ListAppFragment.this.mListView.getChildCount());
        {
        	AppInfo appInfo = (AppInfo)ListAppFragment.this.mListAppAdapter.getItem(position);
        	Intent intent = new Intent(ListAppFragment.this.getActivity(),DetailActivity.class);
        	intent.putExtra(Constant.INTENT_EXTRA_APP_ID, appInfo.getId());
            intent.putExtra(Constant.INTENT_EXTRA_APP_NAME, appInfo.getName());
            intent.putExtra(Constant.INTENT_EXTRA_APP_CAT, appInfo.getCategory());
            intent.putExtra(Constant.INTENT_EXTRA_APP_SIZE, appInfo.getSize());            
            intent.putExtra(Constant.INTENT_EXTRA_APP_AGENCY, appInfo.getCompanyName());
            intent.putExtra(Constant.INTENT_EXTRA_APP_ICON, appInfo.getIconLink());
            //intent.putExtra(Constant.INTENT_EXTRA_APP_RATE, appInfo.get);
            
            ListAppFragment.this.getActivity().startActivity(intent);
            ListAppFragment.this.getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.disappear);
            
        }
    }
  };
  
  private AbsListView.OnScrollListener mOnScrollListener = new Pager(new Runnable()
  {
    public void run()
    {
      if ((ListAppFragment.this.mLoader != null) && (!ListAppFragment.this.mLoader.isLoading()) && (!ListAppFragment.this.mIsReachedBottom))
      {    	
    	  ListAppFragment.this.mLoader.nextPage();
    	  ListAppFragment.this.mLoader.forceLoad();
      }
    }
  });
    
  public void setListAppType(String type)
  {
      this.mListAppType = type;
  }
  
  public void setCategory(String catid)
  {
      this.mCategoryId = catid;
  }
  public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
  {
	  
	DisplayMetrics metrics = new DisplayMetrics();  
	ListAppFragment.this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); 
	
    //View localView = paramLayoutInflater.inflate(R.layout.listapp_fragment, paramViewGroup, false);
	View localView = paramLayoutInflater.inflate(R.layout.common_list_view, paramViewGroup, false);
    this.mListView = ((ListView)localView.findViewById(R.id.list));
    this.mListAppAdapter = new ListAppAdapter(getActivity(),metrics);
    this.mListView.setAdapter(this.mListAppAdapter);
    this.mListView.setOnItemClickListener(this.mItemClickListner);
    this.mListView.setOnScrollListener(this.mOnScrollListener);
    //this.mListView.setOnScrollListener(new PageScrollListener(null));
    this.mLoadingView = ((EmptyLoadingView)localView.findViewById(R.id.loading));
    this.mLoadingView.setRefreshable(this);
    this.mLoadingView.setEmptyText(getString(R.string.app_comments_empty));
   // this.mLoadingView.setDefaultSuccessText(getString(R.string.app_comments_empty));
    this.mLoadingView.setNoNewDataCallback(new EmptyLoadingView.NoNewDataCallback()
    {
      public boolean onNoNewData()
      {
    	  ListAppFragment.this.mIsReachedBottom = true;
         return false;
      }
    });
    return localView;
  }
  
  public void onActivityCreated(Bundle paramBundle)
  {
    super.onActivityCreated(paramBundle);
    getLoaderManager().initLoader(LISTAPP_LOADER, null, this);
   // initLoader(TopApp_LOADER,null,this,getLoaderManager());
  }
  @Override
  public void onStart(){
      super.onStart();
    
  }
  
  
  
  
  /* (non-Javadoc)
 * @see android.support.v4.app.Fragment#onResume()
 */
@Override
public void onResume() {
	// TODO Auto-generated method stub
	super.onResume();
	this.mListAppAdapter.notifyDataSetChanged();
}

@Override
  public Loader onCreateLoader(int id, Bundle bundle) {
  	Log.v("onCreateLoader called");
  	if (id == LISTAPP_LOADER)
      {
        this.mLoader = new ListAppLoader(getActivity());
        ((ListAppLoader)this.mLoader).setCategoryID(this.mCategoryId);
        ((ListAppLoader)this.mLoader).setListAppType(this.mListAppType );
        ((ListAppLoader)this.mLoader).setmAgencyId(mAgencyId);        
        this.mLoader.setProgressNotifiable(this.mLoadingView);
      }
  	BaseLoader baseLoader = this.mLoader; 
      
     return baseLoader;
  }


  @Override
  public void onLoaderReset(Loader<Result> arg0) {
  	// TODO Auto-generated method stub
  	
  }


@Override
public void onLoadFinished(Loader<Result> loader, Result data) {
	// TODO Auto-generated method stub
	if(data != null && !data.mTopAppInfos.isEmpty())
	 this.mListAppAdapter.updateData(data.mTopAppInfos);
}

@Override
public void refreshData() {
	// TODO Auto-generated method stub
	
}

public void setAgencyId(String mAppAgency) {
	mAgencyId = mAppAgency;
}

  
}

